package com.olc.railing.base;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.UserHandle;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.olc.railing.model.UserInfo;
import com.olc.railing.orm.UserInfoDao;
import com.olc.railing.utils.CrashHandler;
import com.olc.railing.utils.LogcatHelper;
import com.tyxh.paylibrary.model.ETCPay;
import com.tyxh.paylibrary.model.ETCPaySceneFactory;
import com.tyxh.paylibrary.model.GasPayBusiness;
import com.tyxh.paylibrary.model.SceneType;
import com.tyxh.paylibrary.utils.NTPClient;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;


public class BaseApplication extends Application {
    private static Application mApplication;
    //是否首次识别车牌
    public static boolean isFristRecogisePlate = true ;
    public static String SD_PATH ;


    //653faf8c6462450552e8708de9e17e65
    //Terminal
    //610300000001 停车场
     //610050125024 加油站
    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        SD_PATH = getOutputFile();
        MultiDex.install(this);
      //  NTPClient.getInstance().initNTPClient("202.120.2.101",2000);
        CrashHandler.getInstance().init(this);
    }


    public static Application getApplication() {
        return mApplication;
    }


    /*
     * 获取手机外部存储路径
     * */
    private String getOutputFile() {
        String removePath = "";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            removePath = getPhysicalExternalFilePathAboveM();
        }
        if(TextUtils.isEmpty(removePath)){
            boolean OutputExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
            if (OutputExist) {
                File mediaFile = Environment.getExternalStorageDirectory();
                removePath =  mediaFile.toString();
            }
        }
        return removePath;
    }

    /**
     * 6.0使用此方法获取外置SD卡路径，尝试过反射
     * 但StorageVolume非Public API 编译不通过（7.0改为公开API）,故使用UserEnvironment
     * 的内部方法getExternalDirs获取所有的路径，通过{@link Environment#isExternalStorageRemovable(File)}
     * 判断若removable则为外部存储
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private static String getPhysicalExternalFilePathAboveM(){
        try {
            //===========================获取UserEnvironment================
            Class<?> userEnvironment = Class.forName("android.os.Environment$UserEnvironment");
            Method getExternalDirs =userEnvironment.getDeclaredMethod("getExternalDirs");
            getExternalDirs.setAccessible(true);
            //========获取构造UserEnvironment的必要参数UserId================
            Class<?> userHandle = Class.forName("android.os.UserHandle");
            Method myUserId = userHandle.getDeclaredMethod("myUserId");
            myUserId.setAccessible(true);
            int mUserId = (int) myUserId.invoke(UserHandle.class);
            Constructor<?> declaredConstructor = userEnvironment.getDeclaredConstructor(Integer.TYPE);
            // 得到UserEnvironment instance
            Object instance = declaredConstructor.newInstance(mUserId);
            File[] files = (File[]) getExternalDirs.invoke(instance);
            for (int i = 0; i < files.length; i++) {
                if (Environment.isExternalStorageRemovable(files[i])){
                    return files[i].getPath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getSourceFileDir(){
        return  SD_PATH + "/recordVideo/";
    }

    public static String getDestinationFileDir(){
        return  SD_PATH +"/railing/";
    }


}
